package com.nicolasprof.profgallery.infopanel {
	import gs.easing.Sine;
	import gs.TweenLite;

	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.assets.InfopanelBackground;

	import flash.display.*;
	import flash.text.*;

	/**
	 * @author Nicolas Prof
	 */
	public class InfoPanel extends Sprite {
		
		private var _bgPanel : InfopanelBackground;
		private var _infoHolder : Sprite;
		private var _title:TextField;
		private var _desc:TextField;
		private var _titleFormat:TextFormat;
		private var _descFormat:TextFormat;
		private var _viewWidth : int;
		private var _viewHeight : int;
		private var _isEnabled : Boolean;
		private var _mask : Sprite;

		
		
		private const TEXT_MARGIN: Number = 10;
		private const SAFE_AREA: Number = 5;

		
		public function InfoPanel() {
		}
		
		protected function animateIn(speed : Number = 0.5) : void {
			TweenLite.to(_infoHolder, speed, {y:-_bgPanel.height});
		}
		protected function animateOut(speed : Number = 0.5) : void {
			TweenLite.to(_infoHolder, speed, {y:SAFE_AREA, ease: Sine.easeInOut});
		}

		protected function drawBackground() : void {
			_bgPanel.width = _viewWidth;
			_bgPanel.height = _viewHeight;
			
			_mask.graphics.beginFill(0xFF0000);
			_mask.graphics.drawRect(0,0,_viewWidth,_viewHeight);
			_mask.graphics.endFill();
		}

		public function initiate($width : Number, $height : Number) : void {
			var config : XML = Engine.CONFIG_XML;
			_viewWidth = $width;
			_viewHeight = $height;
			_isEnabled = false;
			_bgPanel = new InfopanelBackground();
			_mask = new Sprite();
			_infoHolder = new Sprite();
			
			//INFO PANEL TEXTFIELDS AND TEXTFORMATS
			
			_titleFormat = new TextFormat(
										String(config.infopanel.@font), 
										Number(config.infopanel.@textTitleSize), 
										int(config.infopanel.@textColor));
			
			_descFormat = new TextFormat(
										String(config.infopanel.@font), 
										Number(config.infopanel.@textDescSize), 
										int(config.infopanel.@textColor));
			
			_title = new TextField();
			_title.antiAliasType = AntiAliasType.ADVANCED;
			_title.autoSize = TextFieldAutoSize.LEFT;
			_title.defaultTextFormat = _titleFormat;
			_title.selectable = false;
			_title.x = TEXT_MARGIN;
			
			_desc = new TextField();
			_desc.antiAliasType = AntiAliasType.ADVANCED;
			_desc.autoSize = TextFieldAutoSize.LEFT;
			_desc.defaultTextFormat = _descFormat;
			_desc.multiline = true;
			_desc.wordWrap = true;
			_desc.width = _viewWidth - 2*TEXT_MARGIN;
			_desc.selectable = false;
			_desc.x = TEXT_MARGIN;
			
			addChild(_infoHolder);
			addChild(_mask);
			
			_infoHolder.addChild(_bgPanel);
			_infoHolder.addChild(_title);
			_infoHolder.addChild(_desc);
			
			this.mouseChildren = false;
			this.mouseEnabled = false;
			
			_infoHolder.mask = _mask;
			
			drawBackground();
			update();
			animateOut(0);
		}
		public function toggle($state : Boolean) : void {
			_isEnabled = $state;			
			
			if($state){
				animateIn();
			}else{
				animateOut();
			}
		}
		public function update():void{
			_title.y = TEXT_MARGIN;
			_desc.y =  (1.5 * TEXT_MARGIN + _title.height) >> 0;
			var h : Number = 2.5 * TEXT_MARGIN + _title.height + _desc.height;
			_mask.y = -h;
			_bgPanel.height = _mask.height = h;
		}

		/**
		 * Getters / Setters
		 */
		/**
		 * Object
		 * title [String]
		 * desc [String]
		 */
		public function set text(obj : Object) : void {
			_title.text = obj.title;
			_desc.text = obj.desc;
			update();
		}
	}
}
